﻿using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using TaskHub.LocalStorage;
using TaskHub.EntityDataModel;
using TaskHub.Controller;

namespace TaskHub
{
    public partial class NewTask : PhoneApplicationPage
    {
        #region Contructors

            public NewTask()
            {
                InitializeComponent();
                loadTarget();
            }

        #endregion

            #region Properties
            ApplicationDataController AppData = (Application.Current as App).AppData;
            private ucProgress _Progress;
            private ucProgress Progress
            {
                get
                {
                    if (_Progress == null)
                    {
                        _Progress = new ucProgress();
                    }
                    return _Progress;
                }
            }
            #endregion

            #region Events
            private void saveTask(object sender, EventArgs e)
            {
                // Get IDs //
                int myID = IsolatedStorageDAO.GetMyId();
                ItemViewModel target = (ItemViewModel)lpTarget.SelectedItem;
                int targetID = target.contactAccountId;
                if (targetID <= 0) targetID = IsolatedStorageDAO.GetMyId();
                DateTime dueDate = new DateTime ();
                if (dtDueDate.Value != null)
                    dueDate = DateTime.Parse(dtDueDate.Value.ToString());
                char status = '0'; // Waiting

                // Call service.AddTask //
                TaskHub.wsTask.TaskSoapClient ws = new TaskHub.wsTask.TaskSoapClient();
                ws.AddTaskAsync((uint) myID, (uint) targetID, txtName.Text, dueDate, txtDescription.Text);
                ws.AddTaskCompleted += new EventHandler<wsTask.AddTaskCompletedEventArgs>(ws_AddTaskCompleted);

                this.NavigationService.GoBack();                
            }

            void ws_AddTaskCompleted(object sender, wsTask.AddTaskCompletedEventArgs e)
            {
                // Something bad happened!!! //
                if (e.Error != null)
                {
                    MessageBox.Show("Could not save the new Task", "Error", MessageBoxButton.OK);
                    return; 
                }
                
                // Add local task //
                TaskHub.wsTask.TaskList createdTask = e.Result;
                TaskEDM task = new TaskEDM((int)createdTask.OwnerAccountID, (int)createdTask.TargetAccountID, createdTask.Name,
                        createdTask.Description, createdTask.DueDate, createdTask.Status);
                if (createdTask.OwnerAccountID == createdTask.TargetAccountID)
                    IsolatedStorageDAO.AddMyTask(task);
                else
                    IsolatedStorageDAO.AddOtherTask(task);

                AppData.MyTasks.Add(new ItemViewModel()
                {
                    TaskID = (int) createdTask.TaskID,
                    OwnerAccountID = (int) createdTask.OwnerAccountID,
                    TargetAccountID = (int) createdTask.TargetAccountID,
                    TaskStatus = createdTask.Status.ToString (),
                    LineOne = task.Description,
                    LineTwo = task.ObservationText,
                    LineThree = task.DueDate.ToShortDateString(),
                });
 
            }
        #endregion

        #region Methods
        
            private void loadTarget() {
                List<ContactEDM> contacts = (List<ContactEDM>) IsolatedStorageDAO.GetContacts();

                var listContacts = new ObservableCollection<ItemViewModel>();
                listContacts.Add(new ItemViewModel() { contactAccountId = IsolatedStorageDAO.GetMyId(), contactName = "myself" });
                foreach (ContactEDM contact in contacts) {
                    listContacts.Add(new ItemViewModel() { contactAccountId = contact.ContactAccountId, contactName  = contact.Name });
                }
                this.lpTarget.ItemsSource = listContacts;
            }

            void ws_GetContactsCompleted(object sender, wsContact.GetContactsCompletedEventArgs e)
            {
                if (e.Error == null && e.Result != null)
                {
                    ObservableCollection< wsContact.ContactList> listContact1 = new ObservableCollection<wsContact.ContactList>();
                    listContact1 = e.Result;
                }
            }
        #endregion

            private void cancel(object sender, EventArgs e)
            {
                NavigationService.GoBack();
            }

    }
}